from django.conf import settings
from django.core.urlresolvers import reverse
from django.core.mail import send_mail
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.auth.models import User
from human.register.forms import RegistrationForm
from human.register.models import *
#from django.core.context_processors import csrf
#from registration.forms import RegistrationForm
#from registration.models import RegistrationProfile

def register(request, form_class=RegistrationForm, 
             template_name='registration/registration_form.html'):
    """
    Allow a new user to register an account.
    
    By default, ``registration.forms.RegistrationForm`` will be used
    as the registration form; to change this, pass a different form
    class as the ``form_class`` keyword argument. The form class you
    specify must have a method ``save`` which will create and return
    the new ``User``, and that method must accept the keyword argument
    ``profile_callback`` (see below).
    
    """
#    c = {}
#    c.update(csrf(request))
    if request.method == 'POST':
        form = form_class(data=request.POST, files=request.FILES)
        if form.is_valid():
           name=form.cleaned_data['username']
           department=form.cleaned_data['department']
           institute=form.cleaned_data['institute']
           web=form.cleaned_data['web']
           email=form.cleaned_data['email']
#           password=form.cleaned_data['password1']
           new_user = Ehmnuser.objects.create(username=name, web=web, department=department,institute=institute,email=email)
           new_user.save()
           username=name.replace(' ','_').lower()
           subject = 'new EHMN user'
           message = name+'\n'+department+'\n'+institute+'\n'+email+'\n'+web+'\n'
           message += 'to active the account click the link below or copy and paste it into your web browser:\n'+'http://csb.inf.ed.ac.uk/account/activate/'+username
           send_mail(subject, message, 'ehmncsb@gmail.com', ['ehmncsb@gmail.com'], fail_silently=False)
           new_user = User.objects.create_user(username, email, 'ehmncsb')
           new_user.is_active = False
           new_user.save()
#           subject = 'account activation'
#           message = 'click the link below or copy and paste it into your web browser:\n'+'http://csb.inf.ed.ac.uk/account/activate/'+username
#           send_mail(subject, message, 'hma2@inf.ed.ac.uk', ['hma2@inf.ed.ac.uk'], fail_silently=False)
           return HttpResponseRedirect('complete/')
#           return render_to_response(template_name, { 'form': form })
    else:
        form = form_class()
    return render_to_response(template_name, { 'form': form },context_instance=RequestContext(request))

def activate(request, activation_key):
    user=User.objects.get(username=activation_key)
    user.is_active = True
    user.save()
    subject = 'account activation'
    message = 'username:'+user.username+'\npassword: ehmncsb\n'
    send_mail(subject, message, 'ehmncsb@gmail.com', [user.email,'ehmncsb@gmail.com'], fail_silently=False)
    return HttpResponseRedirect('/account/activated/')
   
